import 'dart:typed_data';

import 'package:flutter_test/flutter_test.dart';
import 'package:client_core/crypto.dart';
import 'package:client_core/utils.dart' as Utils;

// test case for AesCtr
void main() async {
  test("test dh case 1", () {
    var alice = DhExchange.fromDhGroup(DhGroup2048());

    var a = Uint8List.fromList([
      164,
      110,
      15,
      148,
      56,
      111,
      27,
      45,
      189,
      114,
      87,
      192,
      35,
      228,
      91,
      138,
      62,
      222,
      24,
      241,
      213,
      108,
      44,
      4,
      116,
      239,
      92,
      52,
      137,
      136,
      189,
      249,
      214,
      178,
      91,
      127,
      95,
      82,
      237,
      8,
      171,
      235,
      122,
      71,
      101,
      154,
      76,
      6,
      243,
      40,
      126,
      226,
      56,
      183,
      107,
      219,
      103,
      251,
      71,
      47,
      120,
      222,
      163,
      186,
      93,
      26,
      79,
      144,
      253,
      31,
      113,
      87,
      230,
      27,
      135,
      171,
      21,
      47,
      218,
      87,
      174,
      89,
      174,
      22,
      174,
      128,
      155,
      116,
      38,
      219,
      176,
      200,
      133,
      103,
      158,
      57,
      65,
      59,
      226,
      214,
      89,
      73,
      194,
      68,
      55,
      94,
      60,
      227,
      204,
      15,
      72,
      159,
      213,
      154,
      159,
      94,
      173,
      183,
      207,
      190,
      56,
      43,
      108,
      175,
      116,
      71,
      98,
      86,
      96,
      148,
      200,
      69,
      147,
      132,
      223,
      12,
      123,
      204,
      242,
      153,
      56,
      112,
      41,
      79,
      220,
      220,
      65,
      204,
      115,
      15,
      71,
      64,
      29,
      148,
      137,
      49,
      152,
      182,
      7,
      206,
      4,
      110,
      108,
      180,
      148,
      98,
      201,
      246,
      213,
      107,
      149,
      153,
      120,
      192,
      88,
      131,
      75,
      182,
      88,
      80,
      154,
      116,
      62,
      120,
      167,
      202,
      54,
      11,
      228,
      2,
      209,
      134,
      52,
      163,
      24,
      77,
      212,
      167,
      206,
      89,
      19,
      0,
      247,
      197,
      96,
      247,
      206,
      122,
      107,
      191,
      36,
      89,
      106,
      203,
      247,
      105,
      211,
      8,
      247,
      23,
      228,
      210,
      241,
      167,
      219,
      31,
      39,
      162,
      231,
      83,
      244,
      69,
      46,
      111,
      15,
      161,
      214,
      141,
      133,
      72,
      14,
      106,
      55,
      196,
      30,
      92,
      211,
      214,
      79,
      207,
      118,
      106,
      170,
      156,
      127
    ]);
    var ga = Uint8List.fromList([
      79,
      4,
      244,
      28,
      61,
      96,
      91,
      105,
      18,
      255,
      204,
      241,
      0,
      199,
      126,
      137,
      144,
      69,
      235,
      245,
      211,
      248,
      189,
      133,
      73,
      4,
      79,
      142,
      241,
      138,
      8,
      30,
      188,
      111,
      162,
      129,
      37,
      243,
      132,
      255,
      49,
      3,
      59,
      68,
      148,
      51,
      77,
      27,
      68,
      93,
      150,
      134,
      178,
      80,
      222,
      87,
      58,
      232,
      50,
      80,
      145,
      204,
      240,
      67,
      108,
      50,
      163,
      167,
      160,
      26,
      29,
      205,
      82,
      162,
      29,
      126,
      45,
      39,
      128,
      32,
      141,
      184,
      231,
      219,
      197,
      18,
      149,
      40,
      71,
      15,
      204,
      189,
      175,
      230,
      147,
      107,
      243,
      95,
      236,
      61,
      123,
      232,
      6,
      159,
      51,
      40,
      31,
      219,
      116,
      154,
      156,
      48,
      48,
      65,
      240,
      140,
      186,
      162,
      183,
      102,
      86,
      63,
      151,
      173,
      27,
      97,
      124,
      170,
      254,
      100,
      6,
      156,
      186,
      170,
      226,
      234,
      80,
      205,
      119,
      227,
      223,
      171,
      250,
      245,
      102,
      5,
      224,
      220,
      7,
      150,
      13,
      144,
      150,
      0,
      147,
      229,
      199,
      162,
      226,
      81,
      49,
      222,
      76,
      136,
      147,
      241,
      21,
      10,
      173,
      26,
      186,
      33,
      23,
      147,
      66,
      158,
      214,
      115,
      237,
      219,
      139,
      99,
      11,
      3,
      131,
      241,
      64,
      174,
      146,
      50,
      43,
      157,
      114,
      220,
      235,
      172,
      184,
      1,
      185,
      125,
      44,
      234,
      15,
      120,
      94,
      6,
      19,
      96,
      232,
      126,
      42,
      241,
      84,
      103,
      63,
      187,
      48,
      169,
      186,
      172,
      250,
      228,
      89,
      151,
      57,
      136,
      42,
      30,
      85,
      191,
      91,
      250,
      186,
      94,
      9,
      88,
      109,
      219,
      213,
      8,
      206,
      151,
      113,
      142,
      190,
      48,
      0,
      198,
      247,
      60,
      38,
      167,
      10,
      198,
      48,
      193
    ]);

    alice.a = Utils.bytesToBigInt(a);
    alice.generateA();

    var ga1 = Utils.bigIntToBytes(alice.gA);

    expect(ga, equals(ga1));
  });

  test("test dh case 2", () {
    var alice = DhExchange.fromDhGroup(DhGroup2048());
    var bob = DhExchange.fromDhGroup(DhGroup2048());

    alice.generateA();
    bob.generateA();

    alice.gB = bob.gA;
    alice.computeSecret();
    bob.gB = alice.gA;
    bob.computeSecret();

    expect(Utils.bigIntToBytes(alice.secret),
        equals(Utils.bigIntToBytes(bob.secret)));
  });

  test("test parsePQ", () {
    var t = (p, q, pqData) {
      var pq = parsePQ(pqData);

      return p == pq.p && q == pq.q;
    };

    expect(
      true,
      t(
        1670150809,
        1972854391,
        Uint8List.fromList([45, 186, 16, 160, 24, 144, 43, 31]),
      ),
    );

    expect(
      true,
      t(
        1667414599,
        2091880051,
        Uint8List.fromList([48, 103, 250, 5, 35, 54, 15, 229]),
      ),
    );

    expect(
      true,
      t(
        1696960609,
        1916038661,
        Uint8List.fromList([45, 31, 113, 100, 163, 121, 127, 229]),
      ),
    );
  });
}
